package com.ideal.manage.esms.controller.monitor;

import com.alibaba.fastjson.JSONObject;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.service.StadningBookInfoService;
import com.ideal.manage.esms.service.alarm.AlarmService;
import com.ideal.manage.esms.service.monitor.MonitorService;
import com.ideal.manage.esms.service.project.EquipmentManageService;
import com.ideal.manage.esms.service.project.ProjectManageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统监控
 * Created By Dai On 2018-03-26
 */
@Controller
@RequestMapping("monitor")
public class MonitorController {
    private Logger logger = LoggerFactory.getLogger(MonitorController.class);

    @Resource
    private MonitorService monitorService;
    @Resource
    private ProjectManageService projectManageService;
    @Resource
    private EquipmentManageService equipmentManageService;
    @Resource
    private AlarmService alarmService;
    @Resource
    private StadningBookInfoService stadningBookInfoService;

    /**
     * 系统监控页面
     */
    @RequestMapping("monitor")
    public void monitorPage(Model model, @RequestParam(required = false) Long isQuick, @RequestParam(required = false) String id) {
        List<JSONObject> list = monitorService.getMonitorList(null);
        model.addAttribute("list", list);
        if (isQuick == null) {
            isQuick = 0L;
        }
        model.addAttribute("isQuick", isQuick);
        if (StringUtils.isBlank(id)) {
            id = "";
        }
        model.addAttribute("id", id);
    }

    /**
     * 系统监控页面左边菜单树搜索后的数据
     *
     * @return
     */
    @RequestMapping("monitor_list")
    @ResponseBody
    public List getMonitorList(String name) {
        List<JSONObject> list = monitorService.getMonitorList(name);
        return list;
    }


//    ----------------------------------------- 楼 宇 级 -----------------------------------------

    /**
     * 根据id刷新 可储蓄电量
     *
     * @param id
     * @return
     */
    @RequestMapping("refresh_could_saving_electricity")
    @ResponseBody
    public String refreshCouldSavingElectricity(String id) {
        return monitorService.refreshCouldSavingElectricity(id);
    }

    /**
     * 根据id刷新 总容量
     *
     * @param id
     * @return
     */
    @RequestMapping("refresh_total_electricity")
    @ResponseBody
    public String refreshTotalElectricity(String id) {
        return monitorService.refreshTotalElectricity(id);
    }

    /**
     * 保存台账信息
     *
     * @param file
     */
    @RequestMapping("save_stadning_book_info")
    @ResponseBody
    public Result saveStadningBookInfo(@RequestParam(required = false) MultipartFile file,
                                       @RequestParam(required = false) String file_id, String data, String id) {
        if (StringUtils.isNotBlank(file_id)) {
            file_id = file_id.replace("file", "");
        }
        String message = stadningBookInfoService.save(file, file_id, data, id);
        Result result = new Result();
        result.setType("alert");
        result.setMessage(message);
        result.setStatus(1L);
        return result;
    }

}
